<!DOCTYPE html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>Editor Test</title>
        
        <style>
        </style>
        
        <script type="text/javascript" src="./../../src/tmlib.js"></script>
        <script>
        
            TM.loadScript("dom", "editor");
            
            TM.main(function(){
                var Editor = TM.DOM.Editor;
                
                var editor = Editor("#ta");
                
                editor.insert("<", 9, 10);
                editor.insert(">", 17, 18);
                
                // エンター
                editor.key.down("return", function(){
                    this.autoIndent();
                    this.autoBracket();
                    return false;
                });
                // タブ
                editor.key.down("tab", function(e){
                    (e.shiftKey==false) ? this.indent() : this.outdent();
                    return false;
                });
                
                // 保存
                editor.key.down("Ctrl+s", function(e){
                    alert("保存しました!!");
                    return false;
                });
                
                // 保管
                editor.key.down("space", function(e){
                    if (e.ctrlKey) {
                        return false;
                    }
                });

                // コメントアウト
                editor.key.down("/", function(e){
                    if (e.ctrlKey) { this.comment(); return false; }
                });
                
                // 自動入力
                editor.key.press('{',   function() { this.wrap('{', '}');   return false; });                
                editor.key.press('(',   function() { this.wrap('(', ')');   return false; });
                
                // html エディタ時のみ有効にした方が良い
                // editor.key.press('<',   function() { this.wrap('<', '>');   return false; });
                editor.key.press('\"',  function() { this.wrap('\"', '\"'); return false; });
                editor.key.press('\'',  function() { this.wrap('\'', '\''); return false; });
                
                // Function Key
                editor.key.down("f1",  function(){ console.log("f1"); console.log(this.selection.getWord());  return false; });
                editor.key.down("f2",  function(){ console.log("f2");  return false; });
                editor.key.down("f3",  function(){ console.log("f3");  return false; });
                editor.key.down("f4",  function(){ console.log("f4");  return false; });
                editor.key.down("f5",  function(){ console.log("f5");  return false; });
                editor.key.down("f6",  function(){ console.log("f6");  return false; });
                editor.key.down("f7",  function(){ console.log("f7");  return false; });
                editor.key.down("f8",  function(){ console.log("f8");  return false; });
                editor.key.down("f9",  function(){ console.log("f9");  return false; });
                editor.key.down("f10", function(){ console.log("f10"); return false; });
                editor.key.down("f11", function(){ console.log("f11"); return false; });
                editor.key.down("f12", function(){ console.log("f12"); return false; });
                
                // shortcut tag
                editor.key.down("Ctrl+p", function(){ this.wrap("<p>", "</p>"); return false;});
                editor.key.down("1", function(e){ if (e.shiftKey && e.ctrlKey) { this.wrap("<p>", "</p>");  } return false;} );
                
                // 選択範囲が行き過ぎちゃうのが嫌なので改行位置で止まるよう修正
                editor.key.down("left", function(e){
                    if (e.ctrlKey) {
                        this.selection.start -= 1;
                        var word_head = this.selection.getWordHead();
                        if (word_head != -1) { this.selection.setCursor(word_head); }
                        return false;
                    }
                    
                    if ( this.selection.start==0 ) { return ; }
                    if ( this.value[ this.selection.start-1 ] == '\n' ) {
                        if ( e.shiftKey ) {
                            this.selection.start -= 1;
                        }
                        else {
                            this.selection.setCursor(this.selection.start-1);
                        }
                        return false;
                    }
                    
                    if (e.ctrlKey) {
                        var line_head = this.selection.getLineHead();
                        var prev_text = this.value.slice( line_head, this.selection.start );
                        prev_text = prev_text.replace(/ /g, '');
                        // 全て空白だった場合そこで止める
                        if (!prev_text) {
                            if (e.shiftKey) {
                                this.selection.start = line_head;
                            }
                            else {
                                this.selection.setCursor(line_head);
                            }
                            return false;
                        }
                    }
                    
                });
                
                // editor.event.add("keydown", function(e){
                    // console.log(e.keyCode);
                    // console.log( String.fromCharCode(e.keyCode) );
                    // console.log( );
                // });
//                 
                // editor.event.add("keypress", function(e){
                    // console.log(e.keyCode);
                    // console.log(e.charCode);
                    // console.log( String.fromCharCode(e.charCode) );
                    // console.log( );
                // });
                editor.selection.setCursor(0);
            });

        </script>
        
    </head>
    
    <body>
        <h1>Editor Test</h1>
        <textarea id="ta" rows=32 cols=128 wrap=0ff>
#include "stdio.h"

int main()
{
    printf("Hello, world!\n");
    
    return false;
}
</textarea>
    </body>
</html>





















